package org.wyn.mongodb.dao.impl;

import org.wyn.mongodb.bean.Message;
import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.common.SearchBDProvider;

import com.google.code.morphia.dao.BasicDAO;

/**
 * 留言mongodb实现层
 * @author wyn
 *
 */
public class MessageDao extends BasicDAO<Message,String>{
	public MessageDao(){
		super(SearchBDProvider.getInstance());
	}
	/**
	 * 为userName创建message,一般在新建用户时调用
	 * @param userName
	 * @return
	 */
	public boolean createMessage(String userName){
		save(new Message(userName));
		ensureIndexes();
		return true;
	}
	public static void main(String[] args) {
		UserDao ud = new UserDao();
		MessageDao md = new MessageDao();
		for(User user : ud.createQuery()){
			Message m = md.findOne("userName", user.getUserName());
			if(null == m){
				md.createMessage(user.getUserName());
			}
		}
	}
}
